package org.exia.minisig.swing.view.common;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.exia.minisig.application.Utils;
import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.swing.controller.GUIManager;
import org.exia.minisig.swing.controller.HomeFramMode;
import org.exia.minisig.swing.view.home.HomeFram;

@SuppressWarnings("serial")
public class ResultItem extends JPanel implements MouseListener {
	//VAR
	private DescriptibleElement currentElement;
	private ResultList parent;
	//GET & SET
	public DescriptibleElement getCurrentElement() {
		return currentElement;
	}
	public void setCurrentElement(DescriptibleElement currentElement) {
		this.currentElement = currentElement;
	}
	//CONSTRUCTOR
	public ResultItem(ResultList _parent, DescriptibleElement _element) {
		this.parent = _parent;
		initializeVar(_element);
	}
		
	public void initializeVar(DescriptibleElement _element){
		currentElement = _element;
		this.addMouseListener(this);
		this.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
		if (parent.getParen().getMode() == HomeFramMode.PLACE){
			filleItemPlace();
		} else {
			fillItem();
		}
	}
	//METHODE OK
	public void fillItem(){
		//----------------------COMMUN---------------------//
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//------------------------------------------------------------------------------------//
		gc.weightx=4;
		gc.weighty=1;
		//------------------------------------------------------------------------------------//
		//1
		gc.insets = new Insets(10, 0, 10, 0);
		gc.gridheight = 2;
		gc.weightx = 10;
		gc.gridx=0;
		gc.gridy=0;
		JLabel picLabel = new JLabel();
		if (this.currentElement.getImages().size() > 0){
			picLabel = new JLabel(
				new ImageIcon(Utils.setIconSize(
							Utils.loadImage(this.currentElement.getImages().get(0)), 
							25, 25
						)));
		}
		this.add(picLabel, gc);
		//------------------------------------------------------------------------------------//
		//3
		gc.insets = new Insets(10, 0, 0, 0);
		gc.weightx = 75;
		gc.gridheight = 1;
		gc.gridx=1;
		gc.gridy=0;
		JLabel labelLabel = new JLabel(currentElement.getLabel());
		labelLabel.setVerticalAlignment(SwingConstants.CENTER);
		labelLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(labelLabel, gc);
		//------------------------------------------------------------------------------------//
		//4
		gc.insets = new Insets(0, 0, 10, 0);
		gc.weightx = 25;
		gc.gridheight = 1;
		gc.gridx=1;
		gc.gridy=1;
		JLabel labeltextLong = new JLabel(currentElement.getText());
		this.add(labeltextLong, gc);
		//------------------------------------------------------------------------------------//
	}
	public void filleItemPlace(){
		//----------------------COMMUN---------------------//
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//------------------------------------------------------------------------------------//
		gc.weightx=4;
		gc.weighty=1;
		//------------------------------------------------------------------------------------//
		//1
		gc.gridwidth = 2;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=0;
		JLabel picLabel = new JLabel(this.currentElement.getLabel());
		this.add(picLabel, gc);
		//------------------------------------------------------------------------------------//
		//2
		
		gc.gridwidth = 4;
		gc.gridheight = 2;
		gc.gridx=0;
		gc.gridy=1;
		JLabel labelLabel = new JLabel(currentElement.getText());
		labelLabel.setForeground(Color.black);
		this.add(labelLabel, gc);
		//------------------------------------------------------------------------------------//
		//3
		gc.insets = new Insets(10, 10, 10, 10);
		gc.weightx = 0.5;
		gc.gridwidth = 1;
		gc.gridheight = 3;
		gc.gridx=4;
		gc.gridy=0;
		JButton buttonLoad = new JButton("Charger");
		buttonLoad.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {	
			}
			@Override
			public void mousePressed(MouseEvent arg0) {	
				GUIManager.getInstance().runNavigation((Place)currentElement);
			}
			@Override
			public void mouseExited(MouseEvent arg0) {	
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {	
			}
			@Override
			public void mouseClicked(MouseEvent arg0) {	
			}
		});
		this.add(buttonLoad, gc);
		//------------------------------------------------------------------------------------//
	}
	//OVERRIDE
	@Override
	public void mouseClicked(MouseEvent arg0) {
		if (this.parent.getParen() instanceof HomeFram){
			HomeFram fram = (HomeFram)this.parent.getParen();
			fram.AddMode(false);
			fram.EditMode(false);
		}
		this.parent.childClicked(this);
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		this.setBackground(Color.lightGray);
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		this.setBackground(new Color(238, 238, 238));
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {		
	}
}
